package com.smart.product.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.common.utils.CommonBeanUtils;
import com.smart.product.common.query.ProductQuery;
import com.smart.product.common.vo.BrandVo;
import com.smart.product.common.vo.ProductDetailVo;
import com.smart.product.common.vo.ProductVo;
import com.smart.product.entity.Brand;
import com.smart.product.entity.Product;
import com.smart.product.mapper.BrandMapper;
import com.smart.product.mapper.ProductMapper;
import com.smart.product.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    ProductMapper productMapper;
    @Resource
    BrandMapper brandMapper;

    /**
     * 1 获取商品信息
     * 2 获取品牌信息
     * 3 获取商品属性信息
     * 4 获取商品属性值信息
     * 5 获取商品SKU库存信息
     * 6 商品阶梯价格设置
     * 7 商品满减价格设置
     * 8 商品可用优惠券
     *
     * @param productId
     * @return
     */
    @Override
    public ProductDetailVo getProductDetail(Long productId) {
        ProductDetailVo productDetail = new ProductDetailVo();
        // 1 获取商品信息
        Product product = productMapper.selectById(productId);
        ProductVo productVo = new ProductVo();
        BeanUtils.copyProperties(product, productVo);
        productDetail.setProduct(productVo);
        // 2 获取品牌信息
        Brand brand = brandMapper.selectById(product.getBrandId());
        if (brand != null) {
            BrandVo brandVo = new BrandVo();
            BeanUtils.copyProperties(brand, brandVo);
            productDetail.setBrand(brandVo);
        }
        return productDetail;
    }

    @Override
    public IPage<ProductVo> search(ProductQuery productQuery) {
        IPage<Product> productIPage = productMapper.selectList(productQuery.getKeyword(), productQuery.getCategoryId(), productQuery.getPage(), productQuery.getSize(), productQuery.getSort());
        Page<ProductVo> productVoPage = new Page<>();
        BeanUtils.copyProperties(productIPage, productVoPage);
        List<ProductVo> productVos = CommonBeanUtils.copyListProperties(productIPage.getRecords(), ProductVo::new);
        productVoPage.setRecords(productVos);
        return productVoPage;

    }
}
